package com.ruoyi.screensave.service.impl;

import com.ruoyi.screensave.domain.ScreenSaveTime;
import com.ruoyi.screensave.mapper.ScreenSaveMapper;
import com.ruoyi.screensave.service.IScreenSaveService;
 import net.sf.jsqlparser.util.validation.metadata.DatabaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScreenSaveServiceImpl implements IScreenSaveService {

    @Autowired
    ScreenSaveMapper screenSaveMapper;

    @Override
    public Integer setScreenSaveTime(Integer minute, Boolean showed) {
        List<ScreenSaveTime> screenSaveTimes = screenSaveMapper.getScreenSaveTime();
        if(CollectionUtils.isEmpty(screenSaveTimes)) {
            return screenSaveMapper.setScreenSaveTime(minute, showed);
        }else {
            return screenSaveMapper.updateScreenSaveTime(minute, showed);
        }
    }

    @Override
    public ScreenSaveTime getScreenSaveTime() throws IllegalArgumentException, DatabaseException{
        List<ScreenSaveTime> screenSaveTime = new ArrayList<>();

            screenSaveTime = screenSaveMapper.getScreenSaveTime();
            if(CollectionUtils.isEmpty(screenSaveTime)){
                throw new IllegalArgumentException("还未插入数据");
            }
            Integer count = screenSaveTime.size();
            if (count >1) {
                throw new DatabaseException("数据出错");
            }
            return screenSaveTime.get(0);

    }
}
